package com.javasm.finance.controller;

import com.javasm.finance.entity.CodeAndMsg;
import com.javasm.finance.entity.Menu;
import com.javasm.finance.entity.ReturnEntity;
import com.javasm.finance.entity.User;
import com.javasm.finance.util.RespUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/main")
public class MainController extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        List<Menu> meanList = (List<Menu>) session.getAttribute("menuList");
        ReturnEntity re=new ReturnEntity();
        if (meanList!=null&&meanList.size()>0){
           //查询登录用户名称
            User user = (User) session.getAttribute("user");
            String loginUserName = user.getUserName();
            //设置Map集合
            Map<String, Object> returnMap = new HashMap<>();
            returnMap.put("menuList", meanList);
            returnMap.put("loginUserName", loginUserName);
            re.setReturnCode(CodeAndMsg.QUERY_SUCCESS.getReturnCode());
            re.setReturnMsg(CodeAndMsg.QUERY_SUCCESS.getReturnMsg());
            //包含：登录用户权限数据  登录用户名称
            re.setReturnData(returnMap);
        }else {
            re.setReturnCode(CodeAndMsg.NO_LOGIN.getReturnCode());
            re.setReturnMsg(CodeAndMsg.NO_LOGIN.getReturnMsg());
        }
        RespUtils.handleResp(resp,re);
    }
}
